﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TimbreRecognition.Recognition.Model.Kohenen
{
   public class KohenenNeuron : AbstractNeuron {

        private int x;

        private int y;

        private int winCount;

        public Dictionary<int, int> WinCountPerClass {get; set;}

        public KohenenNeuron()
        {
            this.WinCountPerClass = new Dictionary<int, int>();
        }

        public int getX() {
            return x;
        }

        public void setX(int x) {
            this.x = x;
        }

        public int getY() {
            return y;
        }

        public void setY(int y) {
            this.y = y;
        }

        public override void recalculateOutput(double[] input) {
            double[] weights = getWeights();

            if (input.Length != weights.Length) {
                throw new ArgumentException("Number of inputs should match number of weights");
            }

            double sum = 0;

            for (int i = 0; i < input.Length; i++){
                double inputValue = input[i];
                double weightValue= weights[i];
                sum += Math.Pow(inputValue - weightValue, 2);
            }

            double norma = Math.Sqrt(sum);

            double output = Math.Exp(-norma);

            setOutput(output);
        }

        public int getWinCount() {
            return winCount;
        }

        public void setWinCount(int winCount) {
            this.winCount = winCount;
        }

        public void incWinCount()  {
            winCount++;
        }

        public void IncWinCountForClass(int classIndex)
        {
            if (WinCountPerClass.ContainsKey(classIndex))
            {
                WinCountPerClass[classIndex] += 1;
            }
            else
            {
                WinCountPerClass.Add(classIndex, 1);
            }
        }

        public void ResetWinCountForClasses()
        {
            this.WinCountPerClass.Clear();
        }

        public int GetWinCountForClass(int classIndex)
        {
            return WinCountPerClass.ContainsKey(classIndex) ? WinCountPerClass[classIndex] : 0;
        }
    }
}
